package com.mm.serivce.mp.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mm.domain.taxi.DTaxiTransactionInfo;
import com.mm.mapper.taxi.DTaxiTransactionInfoMapper;
import com.mm.serivce.mp.TaxiTransactionInfoService;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 交易信息表 服务实现类
 * </p>
 *
 * @author JueDream
 * @since 2021-08-25
 */
@Service
public class TaxiTransactionInfoServiceImpl extends ServiceImpl<DTaxiTransactionInfoMapper, DTaxiTransactionInfo> implements TaxiTransactionInfoService {

    private final DTaxiTransactionInfoMapper transactionInfoMapper;

    public TaxiTransactionInfoServiceImpl(DTaxiTransactionInfoMapper transactionInfoMapper) {
        this.transactionInfoMapper = transactionInfoMapper;
    }

    @Override
    public Page<DTaxiTransactionInfo> queryTransactionPage(Integer page, Integer limit, String payCode) {
        Page<DTaxiTransactionInfo> transactionInfoPage = new Page<>(page, limit);
        LambdaQueryWrapper<DTaxiTransactionInfo> queryWrapper = new LambdaQueryWrapper<DTaxiTransactionInfo>();
        if (StrUtil.isNotBlank(payCode)) {
            queryWrapper.like(DTaxiTransactionInfo::getOrderCode, payCode);
        }
        queryWrapper.eq(DTaxiTransactionInfo::getDelFlg, '0');
        transactionInfoPage = transactionInfoMapper.selectPage(transactionInfoPage, queryWrapper);
        transactionInfoPage.setTotal(transactionInfoPage.getRecords().size());
        return transactionInfoPage;
    }

    @Override
    public DTaxiTransactionInfo queryTransactionOne(Integer taxiId) {
        DTaxiTransactionInfo transactionInfo = new DTaxiTransactionInfo();
        if (taxiId > 0) {
            transactionInfo = transactionInfoMapper.selectById(taxiId);
        }
        return transactionInfo;
    }

    @Override
    public DTaxiTransactionInfo queryTransactionByCode(String orderCode) {
        DTaxiTransactionInfo transactionInfo = new DTaxiTransactionInfo();
        LambdaQueryWrapper<DTaxiTransactionInfo> queryWrapper = new LambdaQueryWrapper<DTaxiTransactionInfo>();
        if (StrUtil.isNotBlank(orderCode)) {
            queryWrapper.eq(DTaxiTransactionInfo::getOrderCode, orderCode);
            queryWrapper.eq(DTaxiTransactionInfo::getDelFlg, '0');
            transactionInfo = transactionInfoMapper.selectOne(queryWrapper);
        }
        return transactionInfo;
    }

    @Override
    public boolean removeTransaction(Integer taxiId) {
        boolean flag = false;
        DTaxiTransactionInfo transactionInfo = transactionInfoMapper.selectById(taxiId);
        if (Objects.nonNull(transactionInfo)) {
            transactionInfo.setDelFlg(1);
            Integer result = transactionInfoMapper.updateById(transactionInfo);
            if (result > 0) {
                flag = true;
            }
        }
        return flag;
    }
}
